package com.purong.purong.fragment;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.activity.SysMsgDetActivity;
import com.purong.purong.adapter.SysMsgAdapter;
import com.purong.purong.bean.MsgBean;
import com.purong.purong.bean.SysMsgBean;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import in.srain.cube.views.ptr.PtrCustomHandler;
import in.srain.cube.views.ptr.PtrDefaultCustomHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrPuRongLayout;

/**
 * Created by zengwenjie on 2017/12/20.
 */
public class SysMsgFragment extends BaseFragment {
    private List<MsgBean> msgList =new ArrayList<>();
    private SysMsgAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private PtrPuRongLayout mPtrFrame;
    private LoadingView loadView;
    @Override
    public int getRootViewId() {
        return R.layout.fragment_sys_notice;
    }

    @Override
    public void initView(View rootView) {
        loadView=(LoadingView)rootView.findViewById(R.id.loading_view);
        loadView.setNoResultToast("暂无消息",null);
        loadView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getNotice(false);
            }

            @Override
            public void onRefresh() {

            }
        });
        mPtrFrame = (PtrPuRongLayout) rootView.findViewById(R.id.rotate_header_list_view_frame);
        mPtrFrame.setPtrHandler(new PtrCustomHandler() {
            @Override
            public boolean checkCanDoLoadMore(PtrFrameLayout frame, View content, View footer) {

                if (msgList.size()==0){
                    return false;
                }else {
                    return PtrDefaultCustomHandler.checkContentCanBePulledUp(frame, content, footer);
                }

            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getNotice(true);

            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getNotice(false);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return false;
            }
        });

        mRecyclerView=(RecyclerView)rootView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
        mAdapter=new SysMsgAdapter(getActivity());
        mAdapter.setData(msgList);
        mAdapter.setOnMsgClickListener(new SysMsgAdapter.OnMsgClickListener() {
            @Override
            public void onMsg(MsgBean bean) {
                Intent intent=new Intent(getActivity(),SysMsgDetActivity.class);
                intent.putExtra("MSG",bean);
                startActivity(intent);
                bean.setIs_read("2");
                mAdapter.notifyDataSetChanged();
                setRead(bean.getMessage_id());
            }

        });
        mRecyclerView.setAdapter(mAdapter);
    }
    private void setRead(String id){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("message_id",id);
        request(Contant.MESSAGE_READ, params, true);
    }
    private void getNotice(boolean loadMore){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (loadMore&& msgList !=null&& msgList.size()>0){
            params.put("message_id", msgList.get(msgList.size()-1).getMessage_id());
        }else {
            params.put("message_id","");
        }
        if (!loadMore){
            loadView.loading();
        }
        if (!request(Contant.MESSAGE, params, true)) {
            if (!loadMore)
                loadView.fail();
            if (!hasDestroy())
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (mPtrFrame.isRefreshing()) {
                            mPtrFrame.refreshComplete();
                        }
                    }
                }, 100);
        }
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.MESSAGE)){
            if (mPtrFrame.isRefreshing()) {
                mPtrFrame.refreshComplete();
            }
            if (responseInfo.isRequestOk){
                try{
                    SysMsgBean bean= JSON.parseObject(responseInfo.content,SysMsgBean.class);
                    if (bean!=null&&bean.getMessage_list()!=null){
                        msgList.addAll(bean.getMessage_list());
                        mAdapter.notifyDataSetChanged();
                    }
                    if (msgList.size()==0){
                        loadView.noResult();
                    }else {
                        loadView.finish();
                    }

                }catch (Exception e){
                    if (msgList.size()==0)
                        loadView.fail();
                }
            }else {
                if (msgList.size()==0)
                    loadView.fail();
            }
        }
    }
    @Override
    public void initData() {
        if (PuRongApp.getLoginInfo(getActivity())==null){
            loadView.noResult();
        }else {
            getNotice(false);
        }

    }
}
